<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.pageTitle }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add icon="plus" type="primary" @click="handleAdd">新增</a-button>
        </a-col>
        <a-col :offset="10" :span="8">
          <a-input-search
              v-model="queryParam.search"
              placeholder="请输入品牌名称或编号"
              style="max-width: 300px; min-width: 150px;float: right;"
              @search="onSearch"
          />
        </a-col>
      </a-row>
    </div>
    <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :loading="isLoading"
        :pageSize="15"
        :scroll="{ x: 1300 }"
        rowKey="id"
    >
      <!-- logo -->
      <template v-slot:logo="text">
        <a :href="text" target="_blank" title="点击查看原图">
          <img :src="text" alt="商品图片" height="50" width="50"/>
        </a>
      </template>
      <!-- 概述 -->
      <template v-slot:description="text">
        <p class="description oneline-hide">{{ text }}</p>
      </template>
      <!-- 操作 -->
      <template v-slot:action="text, item">
        <div class="actions">
          <a v-action:edit @click="handleEdit(item)">编辑</a>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </div>
      </template>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh"/>
    <EditForm ref="EditForm" @handleSubmit="handleRefresh"/>
  </a-card>
</template>

<script>
import * as Api from '@/api/brand'
import {STable} from '@/components'
import AddForm from './modules/AddForm'
import EditForm from './modules/EditForm'

export default {
  name: 'Index',
  components: {
    STable,
    AddForm,
    EditForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '品牌编号',
          dataIndex: 'brandNo',
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
        },
        {
          title: '品牌描述',
          dataIndex: 'description',
          scopedSlots: {customRender: 'description'}
        },
        {
          title: '品牌logo',
          dataIndex: 'logo',
          scopedSlots: {customRender: 'logo'}
        },
        {
          title: '更新时间',
          dataIndex: 'lastModificationTime'
        },
        {
          title: '操作',
          fixed: 'right',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        console.log(param)
        return Api.list({...param, ...this.queryParam})
            .then(response => {
              return response
            })
      }
    }
  },
  created() {
  },
  methods: {

    // 新增记录
    handleAdd() {
      this.$refs.AddForm.add()
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    // 删除记录
    handleDelete(item) {
      const self = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted(item['id'])
              .then(result => {
                self.$message.success('操作成功', 1.5)
                self.handleRefresh()
              })
              .finally(result => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 检索查询
    onSearch() {
      this.handleRefresh(true)
    }

  }
}
</script>

<style lang="less" scoped>
.description {
  max-width: 400px;
}
</style>